import axios from "axios";


async function getKwHotPlayList(f){


    await axios.get('/kuwoapi/list/rank').then((r) => {
        let result = []
        for (let playlist of r.data.data.data) {
            result.push(dealkwResult(playlist))
        }
        //console.log("@@@kw",result.length)
        f(result)
    })
}

function dealkwResult(data){
    let {name:name,id:id,img:imgUrl,uname : auth} = data;



    return {
        "name":name,
        "id":id,
        "imgUrl":imgUrl,
        "auth":auth,
        "from":'kuwo'
    }
}

async function getKwPlayListDetail(id,f){
    await axios.get('/kuwoapi/list/detail?pid='+id).then((r) => {

        //console.log(r.data.data)
        let result = dealDetailResult(r.data.data)
        //console.log(result)
        f(result)
    })
}
function dealDetailResult(data){
    //console.log(data)
    let {userName:user,img:img,listencnt:playCount,name:name,id:id,info:description} = data;
    let subscribedCount = ""

    let tags = data.tag.split(",")
    let tracks = data.musicList;
    let r = tracks.map((i) => {

        return{
            'times' : i.songTimeMinutes,
            'name' : i.name,
            'singer' : i.artist,
            'album' : i.album,
            'id' : i.rid,
            'img':i.albumpic
        }
    })


    return {
        user:user,
        tags:tags,
        name:name,
        id:id,
        description:description,
        img:img,
        playCount:playCount,
        subscribedCount:subscribedCount,
        list:r
    }
}

async function getKwPlayMusic(id,f){
    await axios.get('/kuwoapi/play?mid='+id).then((r) => {

        //console.log(r.data)
        if(!r.data.data){
            f(-1)
        }else {
            let result = dealPlayMusicResult(r.data)
            //console.log(result)
            f(result)
        }
    })
}
/**
 * {
 *                     id: "1",
 *                     title: "打上花火",
 *                     info: "DAOKO,米津玄師",
 *                     // resource: require("./resource/DAOKO,米津玄師 - 打上花火.mp3"),
 *                     resource: "",
 *                     time: "04:50",
 *                     img:
 *                         "http://singerimg.kugou.com/uploadpic/softhead/400/20160913/20160913140233132.jpg"
 *                 },
 */
function dealPlayMusicResult(data){

    return {

        resource:data.data.url,

    }
}



async function getKwSearch(keywords,pn,rn,type,f){
    await axios.get(`/kuwoapi/search?keyword=${keywords}&type=${type}&pn=${pn}&rn=${rn}`).then((r) => {

        //console.log(r)
        let result;
        if(type === 0 || type === '0'){
            result = dealSearchSongResult(r.data.data?.list)
            let count = r.data.data?.total;
            result = {v:result, count:parseInt(count)};
        }else if(type === 3 || type === '3'){
            result = dealSearchSingerResult(r.data.data?.list)
        }else if(type === 2 || type === '2'){
            result = dealSearchPlayListResult(r.data.data?.list)
        }else if(type === 1 || type === '1'){
            result = dealSearchAlbumResult(r.data.data)
        }


        f(result)
    })
}

function dealSearchAlbumResult(data){
    //console.log(data)
    let als = data.albumList || []
    return als.map((r) => {

        return {
            "name":r.album,
            "id":r.albumid,
            "img":r.pic,
            "auth":r.artist,
            "from":'kuwo'
        }

    })



}
function dealSearchPlayListResult(data){

    let als = data || []
    return als.map((r) => {

        return {
            "name":r.name,
            "id":r.id,
            "imgUrl":r.img,
            "auth":r.uname,
            "from":'kuwo'
        }

    })

}


function dealSearchSongResult(data) {


    let tracks = data;
    let r = tracks.map((i) => {
        let times = i.songTimeMinutes;
        times = times.replace(":"," : ");
        return{
            'times' : times,
            'name' : i.name,
            'singer' : i.artist,
            'album' : i.album,
            'id' : i.rid,
            'img':i.albumpic,
            'from':'kuwo'
        }
    })
    return r
}
function dealSearchSingerResult(data) {

    //console.log(data)
    let result = data || {artistCount:0,artists:[{}]}
    let map = result.map((r) => {

        return {
            id:r.id,
            name: r.name.replaceAll('&','').replaceAll('nbsp',' ').replaceAll('; ;',';'),
            img: r.pic,
            from:'kuwo'
        }
    });
    return map
}


async function getKwSingerDetail(id,f){
    await axios.get('/kuwoapi/singer/songs?id='+id).then((r) => {


        let result = dealKwSingerDetailResult(r.data.data)
        //console.log(result)
        f(result)
    })
}

function dealKwSingerDetailResult(data) {
    //console.log(data)
    let als = data.albumList || []

    let result = als.map((r) => {
        return{
            img:r.pic,
            id:r.albumid,
            name:r.album,
            auth:r.artist,
            from:'kuwo'
        }
    });

    return result;



}


async function getKwAlbumDetail(id,f){
    await axios.get('/kuwoapi/album?id='+id).then((r) => {

        //console.log(r.data)
        let result = dealAlbumDetailResult(r.data)
        f(result)
    })
}
function dealAlbumDetailResult(data){
    let list = data.data || {musicList:[]}
    let tracks = list.musicList;
    let r = tracks.map((i) => {

        return{
            'times' : i.songTimeMinutes,
            'name' : i.name,
            'singer' : i.artist,
            'album' : i.album,
            'id' : i.rid,
            'img':i.albumpic
        }
    })


    return r;
}

async function getKwRank(f){
    await axios.get('/kuwoapi/rank').then((r) => {

        //console.log(r.data)
        let result = dealRankResult(r.data)
        f(result)
    })
}
function dealRankResult(data){
    let list = data.data || []
    let r = list.map((i) => {

        return{
            name:i.name,
            list:i.list.map((r) => {
                return{
                    "name":r.name,
                    "id":r.sourceid,
                    "imgUrl":r.pic,
                    "auth":r.intro,
                    "from":'kuwo',
                    "top":true
                }
            }),

        }
    })
    return r;
}
async function getKwRankMusicList(id,pn,rn,f){
    await axios.get(`/kuwoapi/rank/musicList?bangId=${id}&pn=${pn}&rn=${rn}`).then((r) => {

        //console.log(r.data)
        let result = dealRankMusicListResult(r.data)
        let count = r.data.data.num;
        f({v:result,count:count})
    })
}
function dealRankMusicListResult(data){
    let list = data.data?.musicList || []
    let r = list.map((i) => {
        return{
            'times' : i.songTimeMinutes,
            'name' : i.name,
            'singer' : i.artist,
            'album' : i.album,
            'id' : i.rid,
            'img':i.albumpic
        }
    })
    return r;
}

async function getKwlyric(id,f){
    await axios.get(`/kuwoapi/lrc?musicId=${id}`).then((r) => {
        //console.log(r.data)
        let result = deallyricResult(r.data)
        f(result.replaceAll('\r',''))
    })
}
function deallyricResult(data){

    let list = data.data.lrclist || []
    let result = '';
    list.map((r) => {
        let time = dealTime(r.time);
        //console.log(time)
        result += time+r.lineLyric+"\n";
    })
    //console.log(result)

    return result;

}

function dealTime(time){
    let fen = parseInt(time/60)
    let miao = parseInt(time) - fen * 60;
    let haomiao = (time - parseInt(time))
    if(fen < 10){
        fen = '0'+fen;
    }
    if(miao<10){
        miao = '0'+miao;
    }
    haomiao = parseInt(haomiao*100);
    if(haomiao<10){
        haomiao = '0'+haomiao;
    }
    return '['+fen+':'+miao+"."+haomiao+']';
}
export {getKwHotPlayList,getKwPlayListDetail,getKwPlayMusic
    ,getKwSearch,getKwSingerDetail,getKwAlbumDetail,getKwRank,
    getKwRankMusicList,getKwlyric

}